package com.doan.covua;


import java.util.HashMap;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;


public class SoundManager {

    private SoundPool                 mSoundPool;
    private HashMap<Integer, Integer> mSoundPoolMap;
    private AudioManager              mAudioManager;
    private Context                   mContext;
    private MediaPlayer               mp;

    public static int                 SOUND_TITLE    = 1;
    public static int                 SOUND_SELECT   = 2;
    public static int                 SOUND_CHECK    = 3;
    public static int                 SOUND_PICEDROP = 4;

    public SoundManager() {

    }

    public void initSounds(Context theContext) {
        mContext = theContext;
        mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
        mSoundPoolMap = new HashMap<Integer, Integer>();
        mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    }

    public void addSound(int index, int resId) {
        mSoundPoolMap.put(index, mSoundPool.load(mContext, resId, 1));
    }

    public void playSound(int index) {

        int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
    }

    public void playLoopedSound(int index) {

        int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1, 1f);
    }

    public void playSoundTitle() {
        if (mp != null) {
            mp.release();
        }
        mp = MediaPlayer.create(mContext, R.raw.title);
        mp.setLooping(true);
        mp.start();
    }

    public void stopSoundTitle() {
        if (mp != null) {
            mp.stop();
        }
    }
}